package com.laf.manage.controller.admin;

import com.laf.common.annotation.Log;
import com.laf.common.core.controller.BaseController;
import com.laf.common.core.domain.AjaxResult;
import com.laf.common.core.page.TableDataInfo;
import com.laf.common.core.text.Convert;
import com.laf.common.enums.BusinessType;
import com.laf.common.utils.ServletUtils;
import com.laf.common.utils.poi.ExcelUtil;
import com.laf.manage.domain.po.Comments;
import com.laf.manage.service.ICommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

import static com.laf.common.core.page.TableSupport.PAGE_NUM;
import static com.laf.common.core.page.TableSupport.PAGE_SIZE;

/**
 * 评论管理Controller
 *
 * @author zian
 * @date 2024-11-09
 */
@Slf4j
@Api(tags = "评论管理管理")
@RestController("adminCommentsController")
@RequestMapping("/admin/comments")
@RequiredArgsConstructor
public class CommentsController extends BaseController {
    @Autowired
    private ICommentsService commentsService;

    /**
     * 批量通过审核
     */
    @ApiOperation("批量通过审核")
    @PreAuthorize("@ss.hasPermi('manage.comments.edit')")
    @PutMapping("/pass")
    public AjaxResult passCommentsByIds(@RequestBody List<Long> ids) {
        log.info("批量通过审核：{}", ids);
        commentsService.passCommentsByIds(ids);
        return success();
    }

    /**
     * 批量通过审核
     */
    @ApiOperation("批量通过未审核")
    @PreAuthorize("@ss.hasPermi('manage.comments.edit')")
    @PutMapping("/notPass")
    public AjaxResult notPassCommentsByIds(@RequestBody List<Long> ids) {
        log.info("批量通过未审核：{}", ids);
        commentsService.notPassCommentsByIds(ids);
        return success();
    }

    /**
     * 查询评论管理列表
     */
    @ApiOperation("查询评论管理列表")
    @PreAuthorize("@ss.hasPermi('manage:comments:list')")
    @GetMapping("/list")
    public TableDataInfo list(@Validated Comments comments) {
        log.info("查询评论列表：{}", comments);
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return commentsService.selectCommentsVOList(comments, pageNum, pageSize);
    }

    /**
     * 导出评论管理列表
     */
    @ApiOperation("导出评论管理列表")
    @PreAuthorize("@ss.hasPermi('manage:comments:export')")
    @Log(title = "评论管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Comments comments) {
        List<Comments> list = commentsService.selectCommentsList(comments);
        ExcelUtil<Comments> util = new ExcelUtil<Comments>(Comments.class);
        util.exportExcel(response, list, "评论管理数据");
    }

    /**
     * 获取评论管理详细信息
     */
    @ApiOperation("获取评论管理详细信息")
    @PreAuthorize("@ss.hasPermi('manage:comments:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(commentsService.getById(id));
    }

    /**
     * 新增评论管理
     */
    @ApiOperation("新增评论管理")
    @PreAuthorize("@ss.hasPermi('manage:comments:add')")
    @Log(title = "评论管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comments comments) {
        return toAjax(commentsService.save(comments));
    }

    /**
     * 修改评论管理
     */
    @ApiOperation("修改评论管理")
    @PreAuthorize("@ss.hasPermi('manage:comments:edit')")
    @Log(title = "评论管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Comments comments) {
        return toAjax(commentsService.updateById(comments));
    }

    /**
     * 删除评论管理
     */
    @ApiOperation("删除评论管理")
    @PreAuthorize("@ss.hasPermi('manage:comments:remove')")
    @Log(title = "评论管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        commentsService.delByIds(Arrays.asList(ids));
        return success();
    }
}
